//
//  IndexViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/3.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "IndexViewController.h"

@interface IndexViewController () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) UIView * bgView;
@property (strong, nonatomic) UICollectionView * collectionview;
@property (strong, nonatomic) NSArray * imgArr;
@property (strong, nonatomic) NSArray * titleArr;
@property (strong, nonatomic) NSMutableArray * typeArr;
@property (strong, nonatomic) NSMutableArray * complianArr;

@end

@implementation IndexViewController

static NSString * UICollectionViewCellID = @"UICollectionViewCell";
static NSString * IndexHeaderTableViewCellID = @"IndexHeaderTableViewCell";
static NSString * IndexHeaderCollectionReusableViewID = @"IndexHeaderCollectionReusableView";
static NSString * IndexCollectionViewCellID = @"IndexCollectionViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self wr_setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.collectionview];
    [self.collectionview registerClass:[IndexHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:IndexHeaderCollectionReusableViewID];
    [self.collectionview registerClass:[IndexCollectionViewCell class] forCellWithReuseIdentifier:IndexCollectionViewCellID];
    [self requestType];
    [self requestComplianType];
}

-(NSMutableArray *)typeArr
{
    if (_typeArr == nil) {
        _typeArr = [NSMutableArray array];
    }
    return _typeArr;
}

-(NSMutableArray *)complianArr
{
    if (_complianArr == nil) {
        _complianArr = [NSMutableArray array];
    }
    return _complianArr;
}

-(void)requestType
{
    WeakSelf;
    NSString * urlStr = [NSString stringWithFormat:@"/rest/wxRecordController/getWxTypeList"];
    [[HTTPClient shareClient] requestWithPath:urlStr AndReturnBlock:^(NSDictionary *dic) {
        NSArray * dataArr = dic[@"data"];
        for (NSDictionary * dics in dataArr) {
            TypeModel * mdoel = [TypeModel modelWithDic:dics];
            [weakSelf.typeArr addObject:mdoel];
        }
    } AndFailMessage:^(NSString *message, NSString *errorCode) {
        
    }];
}

-(void)requestComplianType
{
    WeakSelf;
    NSString * urlStr = [NSString stringWithFormat:@"/rest/tsRecordController/getTsTypeList"];
    [[HTTPClient shareClient] requestWithPath:urlStr AndReturnBlock:^(NSDictionary *dic) {
        NSArray * dataArr = dic[@"data"];
        for (NSDictionary * dics in dataArr) {
            TypeModel * mdoel = [TypeModel modelWithDic:dics];
            [weakSelf.complianArr addObject:mdoel];
        }
    } AndFailMessage:^(NSString *message, NSString *errorCode) {
        
    }];
}

-(UIView *)bgView
{
    if (_bgView == nil) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = SFMNavBarColor;
    }
    return _bgView;
}

-(NSArray *)imgArr
{
    if (_imgArr == nil) {
        _imgArr = [NSArray arrayWithObjects:[UIImage imageNamed:@"list"],[UIImage imageNamed:@"property"],[UIImage imageNamed:@"notice"],[UIImage imageNamed:@"law"],[UIImage imageNamed:@"repair"],[UIImage imageNamed:@"service"],nil];
    }
    return _imgArr;
}

-(NSArray *)titleArr
{
    if (_titleArr == nil) {
        _titleArr = [NSArray arrayWithObjects:@"缴费清单",@"物业招租", @"通知公告",@"法制宣传",@"我要报修",@"客服中心",nil];
    }
    return _titleArr;
}

-(UICollectionView *)collectionview
{
    if (_collectionview == nil) {
        UICollectionViewFlowLayout * floweLayout = [[UICollectionViewFlowLayout alloc] init];
        floweLayout.itemSize = CGSizeMake(WIDTH/2 - 20 , (WIDTH/2 - 20)/1.4);
        floweLayout.minimumLineSpacing = 10;
        floweLayout.minimumInteritemSpacing = 10;
        floweLayout.sectionHeadersPinToVisibleBounds = YES;
        floweLayout.sectionInset = UIEdgeInsetsMake(24, 15, 24, 15);
        floweLayout.sectionHeadersPinToVisibleBounds = NO;
        _collectionview= [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-kTabBarH) collectionViewLayout:floweLayout];
        _collectionview.delegate = self;
        _collectionview.dataSource = self;
        _collectionview.contentInset = UIEdgeInsetsMake(-kStatusBarH, 0, 0, 0);
        if (@available(iOS 11.0, *)) {
            _collectionview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        _collectionview.backgroundColor = SFMColor(255, 244, 239);
        _collectionview.showsHorizontalScrollIndicator = NO;
    }
    return _collectionview;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.titleArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(WIDTH, 170 + kStatusBarH);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(0, 0);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    IndexHeaderCollectionReusableView * head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:IndexHeaderCollectionReusableViewID forIndexPath:indexPath];
    if ([SFMusertype isEqualToString:@"2"]) {
        head.scanBtn.hidden = NO;
        [head.scanBtn addTarget:self action:@selector(sacnBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }else
    {
        head.scanBtn.hidden = YES;
    }
    return head;
}

#pragma mark - 扫一扫
-(void)sacnBtnClick
{
    WeakSelf;
    // 实例化扫描控制器
    HMScannerController *scanner = [HMScannerController scannerWithCardName:nil avatar:nil completion:^(NSString *stringValue) {
        ScanResultViewController * ctr = [[ScanResultViewController alloc] init];
        [weakSelf.navigationController pushViewController:ctr animated:YES];
    }];
    // 设置导航栏样式
    [scanner setTitleColor:[UIColor whiteColor] tintColor:[UIColor greenColor]];
    // 展现扫描控制器
    [self showDetailViewController:scanner sender:nil];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    IndexCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IndexCollectionViewCellID forIndexPath:indexPath];
    cell.img.image = [self.imgArr objectAtIndex:indexPath.row];
    cell.titleLabel.text = [self.titleArr objectAtIndex:indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if (SFMToken == nil) {
//        LoginViewController * ctr = [[LoginViewController alloc] init];
//        [self presentViewController:ctr animated:YES completion:nil];
//    }
    if (indexPath.row == 0) {
        NotPayListViewController * ctr = [[NotPayListViewController alloc] init];
        [self.navigationController pushViewController:ctr animated:YES];
    }else if (indexPath.row == 1)
    {
        PropertyRentViewController * ctr = [[PropertyRentViewController alloc] init];
        [self.navigationController pushViewController:ctr animated:YES];
    }else if (indexPath.row == 2)
    {
        NoticeAdvertismentViewController * ctr = [[NoticeAdvertismentViewController alloc] init];
        [self.navigationController pushViewController:ctr animated:YES];
    }else if (indexPath.row == 3)
    {
        LawViewController * ctr = [[LawViewController alloc] init];
        [self.navigationController pushViewController:ctr animated:YES];
    }else if (indexPath.row == 4)
    {
        PropertyReparisViewController * ctr =[[PropertyReparisViewController alloc] init];
        ctr.typeArr = self.typeArr;
        [self.navigationController pushViewController:ctr animated:YES];
    }else if (indexPath.row == 5)
    {
        PropertySeriviceViewController * ctr = [[PropertySeriviceViewController alloc] init];
        ctr.complianArr = self.complianArr;
        [self.navigationController pushViewController:ctr animated:YES];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.bgView.frame = CGRectMake(0, 0, WIDTH, - scrollView.contentOffset.y);
    self.bgView.backgroundColor = SFMNavBarColor;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
